import java.util.concurrent.CountDownLatch;

public class Test25 {
    /**
     * 使用CountDownLatch
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch count = new CountDownLatch(8);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("准备起跑");
                int s = (int) (Math.random() * 10000);
                try {
                    Thread.sleep(s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                count.countDown();
                System.out.println("撞线成功,用时:" + s);
            }
        };
        for (int i = 0; i < 8; i++) {
            Thread thread = new Thread(runnable);
            thread.start();
        }
        count.await();
        System.out.println("比赛结束");
    }
}
